package routers

import (
	"echo-utils/internal/dao"
	"echo-utils/pkg/app"
	"echo-utils/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type ArticleApiList struct {
	Page     int64  `form:"page" json:"page"`
	Keywords string `form:"keywords" json:"keywords"`
}

type ArticleApiListAll struct {
	Keywords string `form:"keywords" json:"keywords"`
}

type ArticleApi struct{}

func NewArticleApi() *ArticleApi {
	return &ArticleApi{}
}

// 带分页
func (s *ArticleApi) List(c *gin.Context) {
	var sets ArticleApiList
	if err := c.ShouldBind(&sets); err != nil {
		app.NewResponse(c).ToErrorResponse(errcode.InvalidParams)
		return
	}
	if sets.Page < 0 {
		sets.Page = 1
	}
	adao := dao.NewArticleDao()
	list := adao.FindAllWithPage(int(sets.Page), sets.Keywords)
	app.NewResponse(c).ToSuccessResponse(list)
}

// 不带分页
func (s *ArticleApi) ListAll(c *gin.Context) {
	adao := dao.NewArticleDao()
	list := adao.FindAll()
	app.NewResponse(c).ToSuccessResponse(list)
}
